<?php

include_once('ConnectionManager.php');

function getDetails($productId, $startPosition, $count) {
    $query = "SELECT * FROM PRODUCTDETAIL WHERE PRD_ID IN (";
    $query .= "SELECT DISTINCT PRD_ID FROM PRODUCTDETAILVALUE WHERE PRO_ID = " . $productId . ")";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function countDetails($productId) {
    $query = "SELECT * FROM PRODUCTDETAIL WHERE PRD_ID IN (";
    $query .= "SELECT DISTINCT PRD_ID FROM PRODUCTDETAILVALUE WHERE PRO_ID = " . $productId . ")";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->getNumRows();
}

function loadAllDetail() {
    $query = "SELECT * FROM PRODUCTDETAIL ORDER BY PRD_SORTORDER ASC";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function loadDetailValue($proID, $detailID) {
    $query = "SELECT * FROM PRODUCTDETAILVALUE WHERE PRD_ID = $detailID AND PRO_ID = $proID";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return ConnectionManager::getInstance()->fetchArray();
}

function insertDetailValue($value, $detailID, $proID) {
    $query = "INSERT INTO PRODUCTDETAILVALUE (PRD_ID, PRO_ID, PDV_VALUE)
                VALUES ($detailID, $proID, '$value')";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}

function updateDetailValue($value, $detailID, $proID) {
    $query = "UPDATE PRODUCTDETAILVALUE
             SET PDV_VALUE = '$value' WHERE PRD_ID = $detailID AND PRO_ID = $proID";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return result;
}

function getNumberOfDetail() {
    ConnectionManager::getInstance()->setQuery("SELECT * FROM PRODUCTDETAIL");
    ConnectionManager::getInstance()->query();
    return ConnectionManager::getInstance()->getNumRows();
}

function loadDetailList($search, $start_position, $row_per_page) {
	$query = "SELECT PRD_ID, PRD_NAME, PRD_TYPE, PRD_SORTORDER, PRD_DESCRIPTION FROM PRODUCTDETAIL";
	if (isset($search) && $search != '') {
		$query .= " WHERE PRD_NAME LIKE '" . "%$search%'";
	}
	$query .= " ORDER BY PRD_SORTORDER";
	$query .= " LIMIT " . $start_position . ", " . $row_per_page;
	ConnectionManager::getInstance()->setQuery($query);
	return ConnectionManager::getInstance()->query();
}

function deleteAttribute($id) {
	if (isset($id)) {
		$query = "DELETE FROM PRODUCTDETAIL WHERE PRD_ID in (" . $id . ")";
		ConnectionManager::getInstance()->setQuery($query);
		ConnectionManager::getInstance()->query();
	}
}

function loadAttribute($id) {
	ConnectionManager::getInstance()->setQuery("SELECT PRD_ID, PRD_NAME, PRD_TYPE, PRD_SORTORDER, PRD_DESCRIPTION FROM PRODUCTDETAIL WHERE PRD_ID = $id");
	return ConnectionManager::getInstance()->query();
}

function addAttribute($name, $type, $description, $sortOrder) {
	$query = "INSERT INTO PRODUCTDETAIL (PRD_NAME, PRD_TYPE, PRD_DESCRIPTION) VALUES ('$name', $type, '$description')";
	ConnectionManager::getInstance()->setQuery($query);
	ConnectionManager::getInstance()->query();
}

function updateAttribute($id, $name, $type, $description, $sortOrder) {
	$query = "UPDATE PRODUCTDETAIL SET PRD_NAME = '$name', PRD_TYPE = $type, PRD_DESCRIPTION = '$description', PRD_SORTORDER = $sortOrder WHERE PRD_ID = $id";
	ConnectionManager::getInstance()->setQuery($query);
	ConnectionManager::getInstance()->query();
}

?>